# coding: utf-8
from PySide6.QtCore import Qt
from PySide6.QtGui import QPainter, QBrush, QColor
from PySide6.QtWidgets import QLabel, QSizePolicy
from qfluentwidgets import setFont, FluentLabelBase, isDarkTheme, getFont


class TagLabel(QLabel):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        qss = """
        TagLabel {
            background-color: rgba(0, 0, 0, 0.3);
            border: none;
            color: #FFFFFF;
            border-radius: 5px;
            padding: 6px 6px;
        }
        """
        self.setStyleSheet(qss)
        setFont(self, 12)
        self.setAlignment(Qt.AlignmentFlag.AlignCenter)


class BodyLabel(FluentLabelBase):
    def _init(self):
        super()._init()
        self._text = ""
        self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)

    def getFont(self):
        return getFont(14)

    def setText(self, text: str):
        self._text = text
        new_text = self.fontMetrics().elidedText(text, Qt.TextElideMode.ElideRight, self.width())
        super().setText(new_text)

    def text(self) -> str:
        return self._text

    def resizeEvent(self, event):
        self.setText(self._text)
        super().resizeEvent(event)

    def _drawBackground(self) -> None:
        painter = QPainter(self)
        painter.setRenderHint(QPainter.RenderHint.Antialiasing)
        c = 255 if isDarkTheme() else 44
        brush = QBrush(QColor(c, c, c, 40))
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(brush)
        painter.drawRoundedRect(self.rect(), 5, 5)
        painter.end()

    def paintEvent(self, event):
        if self.text() or not self.pixmap().isNull():
            super().paintEvent(event)
        else:
            self._drawBackground()
